package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.KnowledgeBaseDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Sport;
import com.itheima.service.KnowledgeBaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = KnowledgeBaseService.class)
public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {

    @Autowired
    private KnowledgeBaseDao knowledgeBaseDao;

    @Override
    public void addSportList(List<Sport> list) {

        for (Sport sport : list) {
            String name = sport.getName();
            if(name==null || name.length() <= 0){
                continue;
            }
            Sport resultSport = knowledgeBaseDao.findByName(sport.getName());
            if(resultSport!=null){
                knowledgeBaseDao.updateByName(sport);
            }
            if(resultSport==null){
                knowledgeBaseDao.addOneSport(sport);
            }
        }

//        //添加运动表之前先置空前表
//        knowledgeBaseDao.deleteSportList();
//        //然后再添加新表
//        for (Sport sport : list) {
//            knowledgeBaseDao.addSportList(sport);
//        }
    }

    //条件/分页查询
    @Override
    public PageInfo findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        List<Sport> sportList = knowledgeBaseDao.findByCondition(queryString);
        System.out.println("实现类查询出的结果"+sportList);
        return new PageInfo(sportList);
    }

    //添加单个运动
    @Override
    public void addOneSport(Sport sport) {
        knowledgeBaseDao.addOneSport(sport);
    }

    @Override
    public Sport findById(Integer id) {
        return knowledgeBaseDao.findById(id);
    }

    @Override
    public void update(Sport sport) {
        knowledgeBaseDao.update(sport);
    }

    @Override
    public void deleteById(Integer id) {
        knowledgeBaseDao.deleteById(id);
    }
}
